# Title     : TODO
# Objective : TODO
# Created by: Administrator
# Created on: 2019/7/24

library(ggrepel)
library(plyr)
library(gridExtra)
library(scales)
library(egg)
library(optparse)
library(grid)
library(cowplot)
library(tidyverse)
library(jsonlite)
library(extrafont)
library(ggpubr)
library(rstatix)

createWhenNoExist <- function(f) {
  !dir.exists(f) && dir.create(f)
}

option_list <- list(
  make_option("--i", default = "AllMet_Raw.txt", type = "character", help = "metabolite data file"),
  make_option("--g", default = "group.txt", type = "character", help = "sample group file"),
  make_option("--pc", default = "", type = "character", help = "plot config file")
)
opt <- parse_args(OptionParser(option_list = option_list))

sampleInfo <- read_tsv(opt$g) %>%
  rename(SampleID = Sample) %>%
  select(c("SampleID", "ClassNote")) %>%
  mutate(ClassNote = as.character(ClassNote)) %>%
  mutate(ClassNote = factor(ClassNote, levels = unique(ClassNote)))

plotConfigJson <- fromJSON(opt$pc)

sampleColDf <- tibble(ClassNote = plotConfigJson$color$groups, col = plotConfigJson$color$colors)

sampleCols <- sampleColDf %>%
  deframe()

groups <- unique(sampleInfo$ClassNote)

parent <- "./"
outFileName <- paste0(parent, "/Horizontal_Boxplot.pdf")

data <- read_tsv(opt$i) %>%
  rename(Metabolite = 1) %>%
  mutate(Metabolite = factor(Metabolite, levels = unique(Metabolite))) %>%
  gather("SampleID", "Value", -Metabolite) %>%
  inner_join(sampleInfo, by = c("SampleID")) %>%
  filter(ClassNote %in% groups)

# plotPoint <- plotConfigJson$layout$plotPoint %>%
#   as.logical()
plotPoint <- F
jitterAlpha <- plotConfigJson$layout$jitterAlpha %>%
  as.numeric()
outlierAlpha <- plotConfigJson$layout$outlierAlpha %>%
  as.numeric()
boxWidth <- plotConfigJson$layout$boxWidth %>%
  as.numeric()
width <- plotConfigJson$layout$width %>%
  as.numeric()
height <- plotConfigJson$layout$height %>%
  as.numeric()
xFont <- plotConfigJson$font$xFont
yFont <- plotConfigJson$font$yFont
mainTitleFont <- plotConfigJson$font$mainTitleFont
fontFamily <- plotConfigJson$font$fontFamily
baseFamily <- fontFamily
if (baseFamily == "SimSum") {
  font_import(paths = c("/usr/share/fonts/myFonts"), recursive = F, prompt = F)
}
data

group1 <- levels(data$ClassNote)[1]
group2 <- levels(data$ClassNote)[2]

testTb<-read_csv("comparisons.csv")

testTb
minY<-min(data$Value)
limitY <- c(minY,max(testTb$y.position*1.025))

p <- ggplot(data, mapping = aes(x = Metabolite, y = Value)) +
  theme_bw(base_size = 8, base_family = baseFamily) +
  theme(axis.text.x = element_text(size = 12, hjust = 1, vjust = 1, angle = 45),
        axis.text.y = element_text(size = 12), legend.position = 'top',
        axis.title.y = element_text(size = 11), legend.margin = margin(t = 0.3, b = 0.1, unit = 'cm'),
        legend.text = element_text(size = 6), axis.title.x = element_text(size = 11),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        plot.title = element_text(hjust = 0.5, size = mainTitleFont, family = baseFamily),
        panel.border = element_blank(), axis.line = element_line(),
  ) +
  xlab("") +
  ylab("") +
  # geom_boxplot(outlier.size = 0.75, outlier.alpha = 1, width = 0.5)+
  geom_boxplot(aes(fill = ClassNote), outlier.shape = NA, alpha = 0.7, linewidth = 0.25) +
  geom_point(aes(fill = ClassNote), shape = 21, position = position_jitterdodge()) +
  stat_pvalue_manual(testTb, label = "label", bracket.size = 0.3, tip.length = 0.015) +
  scale_fill_manual("", values = sampleCols)+
  scale_y_continuous(limits = limitY)


# if (plotPoint) {
#   p <- p +
#     geom_boxplot(outlier.shape = NA) +
#     geom_jitter(size = 0.75, alpha = jitterAlpha)
# }else {
p <- p
# }

ggsave(outFileName, width = width, height = height)
